﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cricket.LinqData;
using Cricket.LinqModel;

public partial class ViewClient : System.Web.UI.Page
{
    #region Variable Declaration
    IDataRepository _dataObject = new DataRepository();
    Client client;
    List<Client> clients;
    
    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {
        
        if (!Page.IsPostBack)
        {
            HttpCookie cookie = Request.Cookies["Admin"];
            if (cookie == null)
            {
                Response.Redirect("Login.aspx");
            }
            else
            {
                if (Convert.ToInt32(cookie["AdminId"]) == 0 || Convert.ToInt32(cookie["AdminId"]) == -1)
                {
                    Response.Redirect("Login.aspx");
                }


                bindClient();
            }
        }
    }
    private void bindClient()
    {
        client = new Client(_dataObject);
        HttpCookie cookie = Request.Cookies["Admin"];
        clients = new List<Client>();
        if (Convert.ToInt32(cookie["UserType"]) == Convert.ToInt32(Enumeration.UserType.Admin))
        {
            int AdminId = Convert.ToInt32(cookie["AdminId"]);
            List<GetClientsByAdminIdResult> lstClient = new List<GetClientsByAdminIdResult>();
            lstClient = client.GetClientsByAdminId(AdminId);
            if (lstClient.Count > 0)
            {
                rptClient.DataSource = lstClient;
                rptClient.DataBind();
            }
        }
        else
        {
            List<GetAllClientsResult> lstResult = new List<GetAllClientsResult>();
            lstResult = client.GetAllClient();
            if (lstResult.Count > 0)
            {
                rptClient.DataSource = lstResult;
                rptClient.DataBind();
            }
        }
    }
    protected void rptClient_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName.ToString().ToLower().Trim() == "editclient")
        {
            System.Web.UI.WebControls.RepeaterItem row = (System.Web.UI.WebControls.RepeaterItem)(((System.Web.UI.WebControls.Button)e.CommandSource).NamingContainer);
            System.Web.UI.WebControls.Button btnEdit = (System.Web.UI.WebControls.Button)row.FindControl("btnEdit");
            Response.Redirect(btnEdit.PostBackUrl);
        }
        if (e.CommandName.ToString().ToLower().Trim().Equals("deleteclient"))
        {
            System.Web.UI.WebControls.RepeaterItem row = (System.Web.UI.WebControls.RepeaterItem)(((System.Web.UI.WebControls.Button)e.CommandSource).NamingContainer);
            System.Web.UI.WebControls.Label hdnClientId = (System.Web.UI.WebControls.Label)row.FindControl("hdnClientId");
            client = new Client(_dataObject);
            client.Id = Convert.ToInt32(hdnClientId.Text);
            client.DeleteClient();
            divRed.InnerHtml = "<button data-dismiss='alert' class='close'></button><strong>Success! </strong>&nbsp;Record deleted successfully!";
            divRed.Attributes.Add("class", "alert alert-success");
            bindClient();
        }
    }
    protected void rptClient_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {

            System.Web.UI.WebControls.Label hdnClientId = (System.Web.UI.WebControls.Label)e.Item.FindControl("hdnClientId");
            System.Web.UI.WebControls.Label lblCommissionRate = (System.Web.UI.WebControls.Label)e.Item.FindControl("lblCommissionRate");
            System.Web.UI.WebControls.Button btnEdit = (System.Web.UI.WebControls.Button)e.Item.FindControl("btnEdit");

            lblCommissionRate.Text = lblCommissionRate.Text + " %";
            btnEdit.PostBackUrl = "AddClient.aspx?Id=" + hdnClientId.Text;
        }
    }
    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        Response.Redirect("AddClient.aspx");
    }

    protected void drpClient_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (drpClient.SelectedValue == "1")
        {
            client = new Client(_dataObject);
            HttpCookie cookie = Request.Cookies["Admin"];
            clients = new List<Client>();
            if (Convert.ToInt32(cookie["UserType"]) == Convert.ToInt32(Enumeration.UserType.Admin))
            {
                int AdminId = Convert.ToInt32(cookie["AdminId"]);
                List<GetClientsByAdminIdResult> lstClient = new List<GetClientsByAdminIdResult>();
                lstClient = client.GetClientsByAdminId(AdminId);
                if (lstClient.Count > 0)
                {
                    rptClient.DataSource = lstClient;
                    rptClient.DataBind();
                }
                else
                {
                    rptClient.DataSource = null;
                    rptClient.DataBind();
                }
            }
            else
            {
                List<GetAllClientsResult> lstResult = new List<GetAllClientsResult>();
                lstResult = client.GetAllClient();
                if (lstResult.Count > 0)
                {
                    rptClient.DataSource = lstResult;
                    rptClient.DataBind();
                }
                else
                {
                    rptClient.DataSource = null;
                    rptClient.DataBind();
                }
            }
        }
        else
        {
            client = new Client(_dataObject);
            HttpCookie cookie = Request.Cookies["Admin"];
            clients = new List<Client>();
            if (Convert.ToInt32(cookie["UserType"]) == Convert.ToInt32(Enumeration.UserType.Admin))
            {
                int AdminId = Convert.ToInt32(cookie["AdminId"]);
                List<GetInActiveClientsByAdminIdResult> lstClient = new List<GetInActiveClientsByAdminIdResult>();
                lstClient = client.GetInActiveClientsByAdminId(AdminId);
                if (lstClient.Count > 0)
                {
                    rptClient.DataSource = lstClient;
                    rptClient.DataBind();
                }
                else
                {
                    rptClient.DataSource = null;
                    rptClient.DataBind();
                }
            }
            else
            {
                List<GetAllInActiveClientsResult> lstResult = new List<GetAllInActiveClientsResult>();
                lstResult = client.GetAllInActiveClient();
                if (lstResult.Count > 0)
                {
                    rptClient.DataSource = lstResult;
                    rptClient.DataBind();
                }
                else
                {
                    rptClient.DataSource = null;
                    rptClient.DataBind();
                }
            }
        }
    }
}